package com.studentportalen;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class ChooseCourseActivity extends Activity {
	
	int studentid = Global.getStudent();
	String course1;
	String course2; 
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_course);
    
        DBHelper db = Global.getDBHelper();
        
        Cursor rc = db.getStudentRegistered(studentid);
        rc.moveToFirst();
        //int m = rc.getCount();
        
        ArrayList<String> clist1 = new ArrayList<String>();
        ArrayList<String> clist2 = new ArrayList<String>();
        
        clist1.add(rc.getString(1));
        String dup1 = rc.getString(1);
        rc.moveToNext();
        clist2.add(rc.getString(1));
        String dup2 = rc.getString(1);
        
        Cursor c = db.getAllCourses();
        c.moveToFirst();
        int n = c.getCount();
        
        for (int i=0; i<n; i++){
        	if (dup1.equals(c.getString(1))==false){
        		clist1.add(c.getString(1));
        	}
        	if (dup2.equals(c.getString(1))==false){
        		clist2.add(c.getString(1));
        	}
        	c.moveToNext();
        }
        
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, clist1);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, clist2);
        
        Spinner s1 = (Spinner) findViewById(R.id.spinner1);
        Spinner s2 = (Spinner) findViewById(R.id.spinner2);
        
        s1.setAdapter(adapter1);
        s2.setAdapter(adapter2);
        
        s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                course1 = parent.getItemAtPosition(pos).toString();
            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        
        s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                course2 = parent.getItemAtPosition(pos).toString();
            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        
    }
    
    
    public void chooseCourse(View view){
    	
    	DBHelper db = Global.getDBHelper();
    	int id1 = db.getCourseId(course1);
        int id2 = db.getCourseId(course2);
    	
    	if (id1 != id2){
    		db.deleteRegistred(studentid);
    		db.addRegisterdCourse(studentid, id1);
        	db.addRegisterdCourse(studentid, id2);
        	Toast.makeText(getApplicationContext(), "Register done", Toast.LENGTH_LONG).show();
    	}
    	else{
        	Toast.makeText(getApplicationContext(), "You can't register for the same course twice.", Toast.LENGTH_LONG).show();
        }
        
        
        
        /*AlertDialog alertDialog = new AlertDialog.Builder(ChooseCourseActivity.this).create();
		alertDialog.setTitle("Error");
		alertDialog.setMessage("Student id: "+studentid+" course1: "+c.getString(1)+" course2: "+course2);
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which) {
		     //here you can add functions
		  } });
		//alertDialog.setCancelable(false);
		alertDialog.show();*/
        
    }
}
